package com.blacksheep.mtm.service.Impl;

import com.blacksheep.mtm.common.exception.XmallException;
import com.blacksheep.mtm.dao.*;
import com.blacksheep.mtm.pojo.common.DataTablesResult;
import com.blacksheep.mtm.pojo.manager.TbDevice;
import com.blacksheep.mtm.pojo.manager.TbIceConfig;
import com.blacksheep.mtm.pojo.manager.TbIceIn;
import com.blacksheep.mtm.pojo.manager.TbIceOut;
import com.blacksheep.mtm.service.IceService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class IceServiceImpl implements IceService {

    @Autowired
    private TbIceInMapper iceInMapper;
    @Autowired
    private TbIceOutMapper outMapper;
    @Autowired
    private TbUserMapper userMapper;
    @Autowired
    private TbDeviceMapper deviceMapper;

    @Autowired
    private TbIceConfigMapper configMapper;

    @Override
    public DataTablesResult getIceInList(int draw, int start, int length, String search, String minDate, String maxDate, String orderCol, String orderDir) {
        DataTablesResult result=new DataTablesResult();

        PageHelper.startPage(start/length+1,length);
        List<TbIceIn> list = iceInMapper.selectList("%"+search+"%",minDate,maxDate,orderCol,orderDir);
        PageInfo<TbIceIn> pageInfo=new PageInfo<>(list);
        result.setRecordsFiltered((int)pageInfo.getTotal());
        result.setDraw(draw);
        result.setData(list);
        return  result;
    }

    @Override
    public DataTablesResult getIceOutList(int draw, int start, int length, String search, String minDate, String maxDate, String orderCol, String orderDir) {
        DataTablesResult result= new DataTablesResult();
        PageHelper.startPage(start/length+1,length);
        List<TbIceOut> list = outMapper.selectList("%"+search+"%",minDate,maxDate,orderCol,orderDir);
        PageInfo<TbIceOut> pageInfo=new PageInfo<>(list);
        result.setRecordsFiltered((int)pageInfo.getTotal());
        result.setDraw(draw);
        result.setData(list);
        return  result;
    }

    @Transactional
    @Override
    public int inBatch(List<String> ids) {
        String username = SecurityUtils.getSubject().getPrincipal().toString();
        Long userId = userMapper.getUserIdByUsername(username);
        for (String code : ids){
             if(deviceMapper.selectByCode(code) == null){
                 throw new XmallException("编号:"+code+"新增失败，编码错误");
             }
            TbIceIn in = new TbIceIn();
            in.setDeviceCode(code);
            in.setOperator(userId.intValue());
            in.setCreateTime(new Date().getTime());
           int result = iceInMapper.insertSelective(in);
           if (result<=0){
               throw new XmallException("编号:"+code+"新增失败");
           }
        }
        return 0;
    }

    @Override
    @Transactional
    public int outBatch(List<String> ids) {
        String username = SecurityUtils.getSubject().getPrincipal().toString();
        Long userId = userMapper.getUserIdByUsername(username);
        TbIceConfig config = configMapper.selectByPrimaryKey(1);

        for (String code : ids){
            TbDevice device = deviceMapper.selectByCode(code);
            if(device == null){
                throw new XmallException("编号:"+code+"新增失败，编码错误");
            }
            TbIceIn iceIn = iceInMapper.selectLastRecord(code);
            if (iceIn !=null){
                if (StringUtils.equals("BBP",device.getType())){
                    if (iceIn.getCreateTime() + config.getBbpTime().longValue()*60*1000 > new Date().getTime()){
                        throw new XmallException("编号:"+code+"出库失败，距离冷冻完成时间还差"+ ( (iceIn.getCreateTime() + config.getBbpTime().longValue()*60*1000)-new Date().getTime())/60/1000+"分钟");

                    }
                }else if (StringUtils.equals("LBP",device.getType())){
                    if (iceIn.getCreateTime() + config.getLbpTime().longValue()*60*1000 > new Date().getTime()){
                        throw new XmallException("编号:"+code+"出库失败，距离冷冻完成时间还差"+ ( (iceIn.getCreateTime() + config.getLbpTime().longValue()*60*1000)-new Date().getTime())/60/1000+"分钟");
                    }
                }
            }else{
//                throw new XmallException("编号:"+code+"出库失败，冰块");

            }


            TbIceOut out = new TbIceOut();
            out.setDeviceCode(code);
            out.setOperator(userId.intValue());
            out.setCreateTime(new Date().getTime());
            int result = outMapper.insertSelective(out);
            if (result<=0){
                throw new XmallException("编号:"+code+"新增失败");
            }
        }
        return 0;
    }


    @Override
    public TbIceIn alertState(Integer id, Integer state) {
        return null;
    }

    @Override
    public int delInBatch(Integer[] ids) {
        return 0;
    }

    @Override
    public int delOutBatch(Integer[] ids) {
        return 0;
    }
}
